home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / A-B / 003. SillyBalls.cpt / SillyBalls.p < prev    next >
Text File  |  1988-08-01  |  9KB  |  246 lines

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Sample Application
  6. #
  7. #    SillyBalls
  8. #
  9. #    SillyBalls.p    -    Pascal Source
  10. #
  11. #    Copyright © 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    SillyBalls.p        August 1, 1988
  17. #                SillyBalls.c        August 1, 1988
  18. #                PSillyBalls.make    August 1, 1988
  19. #                CSillyBalls.make    August 1, 1988
  20. #
  21. #    This is a very simple sample program that demonstrates how to use Color 
  22. #    QuickDraw.  It is about two pages of code, and does nothing more than open
  23. #    a color window and draw randomly colored ovals in the window.
  24. #    
  25. #    The purpose is to show how to get some initial results with Color QuickDraw.
  26. #    It is a complete program and is very short to be as clear as possible.
  27. #    
  28. #    It does not have an Event Loop.  It is not fully functional in the sense that
  29. #    it does not do all the things you would expect a well behaved Macintosh 
  30. #    program to do, like size the window naturally, have an event loop, use menus, 
  31. #    etc.
  32. #
  33. #    See Sample and TESample for the general structure and MultiFinder techniques that
  34. #    we recommend that you use when building a new application.
  35. #
  36. ------------------------------------------------------------------------------}
  37.  
  38. Program SillyBalls;
  39. (*
  40.     Version 1.0: 6/2/88
  41.     
  42.     purpose        To demonstrate a simple color App using Color QuickDraw.
  43.                         It draws colored balls in a color window, then uses colored
  44.                         text inverted in the ball.  The ball location and color is Random.
  45.                         
  46.                         This program was written by Bo3b Johnson, 1/88.
  47.                         
  48.                         The inverted Bob text was a Skippy Blair special concept,
  49.                         kept for obvious aesthetic reasons.
  50.                         
  51.                         You can build the program with this junk:
  52. pascal SillyBalls.p
  53. Link SillyBalls.p.o ∂
  54.     "{MPW}Libraries:"Interface.o ∂
  55.     "{MPW}Libraries:"Runtime.o ∂
  56.     "{MPW}PLibraries:"Paslib.o ∂
  57.     -o SillyBalls
  58. SillyBalls
  59. *)
  60.  
  61. { Where does it fit:
  62.     This is a series of sample programs for those doing development
  63.     using Color QuickDraw.  Since the whole color problem depends
  64.     upon the exact effect desired, there are a number of answers
  65.     to how to use colors, from the simple to the radically complex.
  66.     These programs try to cover the gamut, so you should use 
  67.     which ever seems appropriate.  In most cases, use the simplest
  68.     one that will give the desired results.  The compatibility
  69.     rating is from 0..9 where low is better.  The more known risks 
  70.     there are the higher the rating.
  71.     
  72.     
  73.     The programs (in order of compatibility):
  74.     
  75.         SillyBalls:    (***)
  76.             This is the simplest use of Color QuickDraw, and does
  77.             not use the Palette Manager.  It draws randomly colored
  78.             balls in a color window.  This is intended to give you
  79.             the absolute minimum required to get color on the screen.
  80.             Written in straight Pascal code.
  81.             Compatibility rating = 0, no known risks.
  82.         
  83.         FracAppPalette:
  84.             This is a version of FracApp that uses only the Palette
  85.             Manager.  It does not support color table animation
  86.             since that part of the Palette Manager is not sufficient.
  87.             The program demonstrates a full color palette that is
  88.             used to display the Mandelbrot set.  It uses an offscreen
  89.             gDevice w/ Port to handle the data, using CopyBits to
  90.             draw to the window.  The Palette is automatically 
  91.             associated with each window.  The PICT files are read
  92.             and written using the bottlenecks, to save on memory
  93.             useage.
  94.             Written in MacApp Object Pascal code.
  95.             Compatibility rating = 0, no known risks.
  96.         
  97.         TubeTest:
  98.             This is a small demo program that demonstrates using the
  99.             Palette Manager for color table animation.  It uses a 
  100.             color palette with animating entries, and draws using the
  101.             Palette Manager.  There are two circles of animating colors
  102.             which gives a flowing tube effect.  This is a valid case
  103.             for using the animating colors aspect of the Palette Manager,
  104.             since the image is being drawn directly.
  105.             Written in straight Pascal code.
  106.             Compatibility rating = 0, no known risks.
  107.         
  108.         FracApp:
  109.             This is the ‘commercial quality’ version of FracApp.  This
  110.             version supports color table animation, using an offscreen
  111.             gDevice w/ Port, and handles multiple documents.  The
  112.             CopyBits updates to the screen are as fast as possible.  The
  113.             program does not use the Palette Manager, except to
  114.             provide for the system palette, or color modes with less than
  115.             255 colors.  For color table animation using an offscreen
  116.             gDevice w/ Port, it uses the Color Manager and handles the
  117.             colors itself.  Strict compatibility was relaxed to allow for
  118.             a higher performance program.  This is the most ‘real’ of the
  119.             sample programs.
  120.             Written in MacApp Object Pascal code.
  121.             Compatibility rating = 2.  (nothing will break, but it may not
  122.             always look correct.)
  123.         
  124.         FracApp300:
  125.             This doesn't support colors, but demonstrates how to create and
  126.             use a 300 dpi bitmap w/ Port.  The bitmap is printed at full
  127.             resolution on LaserWriters, and clipped on other printers (but
  128.             they still print).  It demonstrates how to use a high resolution
  129.             image as a PICT file, and how to print them out.
  130.             Written in MacApp Object Pascal code.
  131.             Compatibility rating = 1.  (The use of PrGeneral is slightly 
  132.             out of the ordinary, although supported.)
  133. }
  134.  
  135. USES    MemTypes, QuickDraw, OSIntf, ToolIntf;
  136.  
  137. CONST
  138.     BallWidth    = 20;
  139.     BallHeight    = 20;
  140.     BobSize        = 8;        { Size of text in each ball. }
  141.  
  142. VAR
  143.     windRect    : Rect;
  144.     
  145.     
  146. { Initialize everything for the program, make sure we can run. }
  147.  
  148. Procedure Initialize;
  149.  
  150. VAR
  151.     mainPtr        : WindowPtr;
  152.     error        : OSErr;
  153.     theWorld    : SysEnvRec;
  154.     
  155. BEGIN
  156.     { Test the computer to be sure we can do color.  If not we would crash, which
  157.     would be bad.  If we can’t run, just beep and exit. }
  158.     error := SysEnvirons(1, theWorld);
  159.     IF NOT theWorld.hasColorQD THEN BEGIN
  160.         SysBeep (50);
  161.         ExitToShell;                        { If no color QD, we must leave. }
  162.     END;
  163.     
  164.     { Initialize all the needed managers. }
  165.     InitGraf(@thePort);
  166.     InitFonts;
  167.     InitWindows;
  168.     InitMenus;
  169.     TEInit;
  170.     InitDialogs(NIL);
  171.     InitCursor;
  172.  
  173.     { To make the Random sequences truly random, we need to make the seed start
  174.     at a different number.  An easy way to do this is to put the current time
  175.     and date into the seed.  Since it is always incrementing the starting seed
  176.     will always be different.  Don’t for each call of Random, or the sequence
  177.     will no longer be random.  Only needed once, here in the init. }
  178.     GetDateTime (RandSeed);
  179.  
  180.     { Make a new window for drawing in, and it must be a color window.  The window is
  181.     full screen size, made smaller to make it more visible. }
  182.     windRect := screenBits.bounds;
  183.     InsetRect (windRect, 50, 50);
  184.     MainPtr := NewCWindow(NIL, windRect, 'Bo3b Land', TRUE, documentProc, 
  185.                             Pointer(-1), FALSE, 0);
  186.         
  187.     SetPort(MainPtr);                        { set window to current graf port   }
  188.     TextSize(BobSize);                        { smaller font for drawing. }
  189. END;    { Initialize }
  190.  
  191.  
  192. { NewBall: make another ball in the window at a random location and color. }
  193.  
  194. Procedure NewBall;
  195.  
  196. VAR
  197.     ballColor    : RGBColor;
  198.     ballRect    : Rect;
  199.     newLeft,
  200.     newTop        : LongInt;
  201.     
  202. BEGIN
  203.     { Make a random new color for the ball. }
  204.     WITH ballColor        DO BEGIN
  205.         red := Random;   green := Random;  blue := Random;
  206.     END;
  207.     
  208.     { Set that color as the new color to use in drawing. }
  209.     RGBForeColor (ballColor);
  210.  
  211.     { Make a Random new location for the ball, that is normalized to the window size.  
  212.     This makes the Integer from Random into a number that is 0..windRect.bottom
  213.     and 0..windRect.right.  They are normalized so that we don't spend most of our
  214.     time drawing in places outside of the window. }
  215.     newTop := Random;    newLeft := Random;
  216.     newTop := ((newTop+32767) * windRect.bottom) DIV 65536;
  217.     newLeft := ((newLeft+32767) * windRect.right) DIV 65536;
  218.     SetRect(ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight);
  219.     
  220.     { Move pen to the new location, and paint the colored ball. }
  221.     MoveTo(newLeft, newTop);
  222.     PaintOval (ballRect);
  223.     
  224.     { Move the pen to the middle of the new ball position, for the text }
  225.     MoveTo(ballRect.Left + BallWidth DIV 2 - BobSize, 
  226.             ballRect.Top + BallHeight DIV 2 + BobSize DIV 2 -1);
  227.     
  228.     { Invert the color and draw the text there.  This won’t look quite right in 1 bit
  229.     mode, since the foreground and background colors will be the same.  Color
  230.     QuickDraw special cases this to not invert the color, to avoid invisible
  231.     drawing. }
  232.     InvertColor(ballColor); 
  233.     RGBForeColor(ballColor);
  234.     DrawString('Bob');
  235. END;    { NewBall }
  236.  
  237.  
  238. BEGIN       { Main body of program SillyBalls }
  239.     Initialize;
  240.     
  241.     Repeat
  242.         NewBall;
  243.     Until Button;
  244.     
  245. END.    { SillyBalls }
  246.